/**
 * Sumpu Inc.
 * Copyright (c) 2011-2012 All Rights Reserved. 
 */
package cn.sumpu.app.guoguojie.biz.impl;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.biz.AppconfBiz;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

/**
 * @author nick
 * @version $Id: AppconfBizImpl.java, v 0.1 2012-9-9 下午02:05:27 nick Exp $
 */
public class AppconfBizImpl implements AppconfBiz {

    private static Logger logger      = LoggerFactory.getLogger(AppconfBizImpl.class);

    private String        appconfFile = null;

    public void setAppconfFile(String appconfFile) {
        this.appconfFile = appconfFile;
    }

    /**
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.Appconf#getGuoguojieCommissionRate()
     */
    @Override
    public double getGuoguojieCommissionRate() throws AppBusinessException {
        Properties prop = loadProperties();
        String rate = prop.getProperty("guoguojie.commission.rate");
        if (!Common.emptyStr(rate)) {
            return Double.parseDouble(rate);
        } else {
            return 1;
        }
    }

    /**
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.Appconf#getGuoguojieCommissionMin()
     */
    @Override
    public double getGuoguojieCommissionMin() throws AppBusinessException {
        Properties prop = loadProperties();
        String min = prop.getProperty("guoguojie.commission.min");
        if (!Common.emptyStr(min)) {
            return Double.parseDouble(min);
        } else {
            return 2;
        }
    }

    /**
     * @param guoguojieCommissionRate
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.Appconf#setGuoguojieCommissionRate(java.lang.String)
     */
    @Override
    public void setGuoguojieCommissionRate(String guoguojieCommissionRate) throws AppBusinessException {
        Properties prop = loadProperties();
        if (!Common.emptyStr(guoguojieCommissionRate)) {
            prop.setProperty("guoguojie.commission.rate", guoguojieCommissionRate);
            storeProperties(prop);
        }
    }

    /**
     * @param guoguojieCommissionMin
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.Appconf#setGuoguojieCommissionMin(java.lang.String)
     */
    @Override
    public void setGuoguojieCommissionMin(String guoguojieCommissionMin) throws AppBusinessException {
        Properties prop = loadProperties();
        if (!Common.emptyStr(guoguojieCommissionMin)) {
            prop.setProperty("guoguojie.commission.min", guoguojieCommissionMin);
            storeProperties(prop);
        }
    }

    /**
     * @return
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.AppconfBiz#getTopSession()
     */
    @Override
    public String getTopSession() throws AppBusinessException {
        Properties prop = loadProperties();
        return prop.getProperty("top.session");
    }

    /**
     * @param topSession
     * @throws AppBusinessException
     * @see cn.sumpu.app.guoguojie.biz.AppconfBiz#setTopSession(java.lang.String)
     */
    @Override
    public void setTopSession(String topSession) throws AppBusinessException {
        Properties prop = loadProperties();
        if (!Common.emptyStr(topSession)) {
            prop.setProperty("top.session", topSession);
            storeProperties(prop);
        }
    }

    private void storeProperties(Properties prop) {
        try {
            prop.store(new BufferedWriter(new java.io.FileWriter(appconfFile)), "");
        } catch (IOException e) {
            if (logger.isErrorEnabled()) {
                logger.error("Can't load appconfFile[" + appconfFile + "]", e);
            }
        }
    }

    private Properties loadProperties() {
        Properties prop = new Properties();
        try {
            prop.load(new BufferedReader(new java.io.FileReader(appconfFile)));
            return prop;
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("Can't load appconfFile[" + appconfFile + "]", e);
            }
            return null;
        }
    }

}
